import { request } from '../../utils/request'

Page({
  data: {
    // 初始数据
    dataList: []
  },
  getDataList() {
    request({
      url: '/mini/api/mine/queryCoupons'
    }).then((resdata) => {
      if (resdata.code == 200) {
        this.setData({
          dataList: resdata.data
        })
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },
  onShow() {
    this.getDataList()
  },
  claimCoupon(item) {
    console.log(
      item.currentTarget.dataset.item.id
    )
    request({
      url: '/mini/api/mine/redeemCoupon',
      method: 'POST',
      data: {
        couponId: Number(
          item.currentTarget.dataset.item.id
        )
      }
    }).then((resdata) => {
      if (resdata.code == 200) {
        wx.showToast({
          title: '优惠券领取成功',
          icon: 'success',
          duration: 2000
        })
        this.getDataList()
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  }
})
